package com.jiulan.hearthearing.ui.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.common.UserData;
import com.jiulan.hearthearing.ui.login.activity.LoginVerificationCodeActivity;
import com.jiulan.hearthearing.widget.NoScrollViewPager;

import java.util.Timer;
import java.util.TimerTask;

@RequiresApi(api = Build.VERSION_CODES.M)
public class MainActivity extends BaseActivity {
    private int from = 0;

    private NoScrollViewPager vpContent;
    private BottomNavigationView navigation;
    private MainPageController pageController;
    boolean isExit = false;//退出程序标识

    @Override
    protected void init(Bundle savedInstanceState) {
        switch (from) {
            case 0:
            case 1:
            case 3:
                setAndroidNativeLightStatusBar(true);
                break;
            case 2:
            case 4:
                setAndroidNativeLightStatusBar(false);
                break;
        }
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        registerMessageReceiver();
        from = getIntent().getIntExtra("from", 0);
        vpContent = findViewById(R.id.vpContent);
        pageController = new MainPageController(MainActivity.this, vpContent);
        navigation = findViewById(R.id.navigation);
        //修改为原图片颜色
        navigation.setItemIconTintList(null);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        switch (from) {
            case 0:
                navigation.setSelectedItemId(R.id.navigation_home);
                break;
            case 1:
                navigation.setSelectedItemId(R.id.navigation_heart);
                break;
            case 2:
                navigation.setSelectedItemId(R.id.navigation_school);
                break;
            case 3:
                navigation.setSelectedItemId(R.id.navigation_union);
                break;
            case 4:
                navigation.setSelectedItemId(R.id.navigation_my);
                break;
        }
    }

    public static LocalBroadcastManager localBroadcastManager;
    private MessageReceiver mMessageReceiver;
    private IntentFilter filter;
    public static String LOCAL_BROADCAST = "com.jiulan.hearthearing.ui.main.MainActivity.MESSAGE_RECEIVED_ACTION";

    public void registerMessageReceiver() {
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        mMessageReceiver = new MessageReceiver();
        filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(LOCAL_BROADCAST);
        localBroadcastManager.registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (LOCAL_BROADCAST.equals(intent.getAction())) {
                    switch (intent.getIntExtra("action", 1)) {
                        case 1://点击首页心译通
                            from = 1;
                            navigation.setSelectedItemId(R.id.navigation_heart);
                            setAndroidNativeLightStatusBar(true);
                            break;
                        case 2://点击首页商学院
                            from = 2;
                            navigation.setSelectedItemId(R.id.navigation_school);
                            setAndroidNativeLightStatusBar(false);
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    /**
     * from = 0，首页
     *
     * @param context
     * @param i
     */
    public static void actionStart(Context context, int i) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("from", i);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(mMessageReceiver);
    }

    /**
     * 底部导航栏点击事件
     */
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    from = 0;
                    pageController.ChangePage(0);
                    setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_heart:
                    from = 1;
                    pageController.ChangePage(1);
                    setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_school:
                    from = 2;
                    pageController.ChangePage(2);
                    setAndroidNativeLightStatusBar(false);
                    return true;
                case R.id.navigation_union:
                    from = 3;
                    pageController.ChangePage(3);
                    setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_my:
                    if (UserData.create(getContext()).isLogin()) {
                        from = 4;
                        pageController.ChangePage(4);
                        setAndroidNativeLightStatusBar(false);
                        return true;
                    } else {
                        LoginVerificationCodeActivity.actionStart(getContext(), "");
                        return false;
                    }
            }
            return false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            switch (from) {
                case 0:
                    exitByDoubleClick();
                    break;
                case 1:
                case 2:
                case 3:
                case 4:
                    from = 0;
                    navigation.setSelectedItemId(R.id.navigation_home);
                    setAndroidNativeLightStatusBar(true);
                    break;
            }
        }
        return false;
    }

    //双击返回退出
    private void exitByDoubleClick() {
        Timer tExit = null;
        if (!isExit) {
            isExit = true;
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;//取消退出
                }
            }, 2000);// 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);
        }
    }
}
